package com.atguigu.dga.meta.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.meta.bean.TableMetaForQuery;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga.meta.bean.TableMetaInfoVO;
import com.atguigu.dga.meta.service.TableMetaInfoExtraService;
import com.atguigu.dga.meta.service.TableMetaInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * <p>
 * 元数据表 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2023-11-18
 */
@RestController
@RequestMapping("/tableMetaInfo")
public class TableMetaInfoController {

    @Autowired
    TableMetaInfoService tableMetaInfoService;

    @Autowired
    TableMetaInfoExtraService tableMetaInfoExtraService;


    AtomicLong pv=new AtomicLong();

    @GetMapping("/table-list")
    public   String  getTableMetaList(TableMetaForQuery tableMetaForQuery){
        pv.getAndIncrement();
        List<TableMetaInfoVO> tableMetaInfoVOList=  tableMetaInfoService.getTableMetaListForQuery(tableMetaForQuery);


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",100);
        jsonObject.put("list",tableMetaInfoVOList);


        return jsonObject.toJSONString();


    }

    @GetMapping("/table/{id}")
    public String getTableMeta(@PathVariable("id")String tableId){
        TableMetaInfo tableMetaInfo = tableMetaInfoService.getById(tableId);

        TableMetaInfoExtra tableMetaInfoExtra = tableMetaInfoExtraService.getOne(new QueryWrapper<TableMetaInfoExtra>().eq("schema_name", tableMetaInfo.getSchemaName())
                .eq("table_name", tableMetaInfo.getTableName()));

        tableMetaInfo.setTableMetaInfoExtra(tableMetaInfoExtra);
        return JSON.toJSONString(tableMetaInfo);
    }


    @PostMapping("/tableExtra")
    public String  saveTableExtra(@RequestBody TableMetaInfoExtra tableMetaInfoExtra){

        tableMetaInfoExtraService.saveOrUpdate(tableMetaInfoExtra);
        return  "success";

    }



}
